import 'package:flutter/material.dart';
import 'dart:async';
import 'package:flutter/services.dart';
import 'package:http_plugin/http_plugin.dart';

void main() {
  runApp(const MyApp());
}

class MyApp extends StatefulWidget {
  const MyApp({super.key});

  @override
  State<MyApp> createState() => _MyAppState();
}

class _MyAppState extends State<MyApp> {
  String _platformVersion = 'Unknown';
  final _httpPlugin = HttpPlugin();

  @override
  void initState() {
    super.initState();
    initPlatformState();
  }

  // Platform messages are asynchronous, so we initialize in an async method.
  Future<void> initPlatformState() async {
    String platformVersion;
    // Platform messages may fail, so we use a try/catch PlatformException.
    // We also handle the message potentially returning null.
    try {
      platformVersion =
          await _httpPlugin.getPlatformVersion() ?? 'Unknown platform version';
    } on PlatformException {
      platformVersion = 'Failed to get platform version.';
    }

    // If the widget was removed from the tree while the asynchronous platform
    // message was in flight, we want to discard the reply rather than calling
    // setState to update our non-existent appearance.
    if (!mounted) return;

    setState(() {
      _platformVersion = platformVersion;
    });
  }

  @override
  Widget build(BuildContext context) {
    setupHttp();
    getExampleMo();
    return MaterialApp(
      home: Scaffold(
        appBar: AppBar(
          title: const Text('Plugin example app'),
        ),
        body: Center(
          child: Text('Running on: $_platformVersion\n'),
        ),
      ),
    );
  }

  setupHttp() {
    ZyHttpClient.instance.setBaseUrl('https://translateapptest.laihua.com');
    ZyHttpClient.instance.setHeaders({'Authorization': 'Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiJlODcyMTM4Ny00NjNmLTQ5MDItOTE5Zi1mNGNiNGUwZTdhM2EiLCJ1c2VybmFtZSI6IklubkFJTy01MjgiLCJ0eXBlIjoidXNlciIsImRldmljZUlkIjoiIiwiaWF0IjoxNzUxODY3MDQ2LCJleHAiOjE3NTQ0NTkwNDZ9.XGw7YxWaQErDoCLgV9BoZ5Vt8n-Wkxvrj2Ozd3Ns8Y4'});
  }
  
  /// 获取示例数据
  Future<void> getExampleMo() async {
    final response = await ZyHttpClient.instance.get<ExampleMo>(path: '/api/v1/user/quotas', fromJson: (json) {
      return ExampleMo.fromJson(json);
    });
    debugPrint(response.data.toString());
  }
}

class ExampleMo {
  final int level;
  final int interpretationTotal;
  final int interpretationUsed;
  final int mindMapTotal;
  final int mindMapUsed;
  final int expirationAt;

  ExampleMo({required this.level, 
    required this.mindMapUsed, 
    required this.mindMapTotal, 
    required this.expirationAt, 
    required this.interpretationUsed, 
    required this.interpretationTotal});

  factory ExampleMo.fromJson(Map<String, dynamic> json) =>
      ExampleMo(level: json['level'], 
      interpretationTotal: json['interpretationTotal'], 
      interpretationUsed: json['interpretationUsed'], 
      mindMapTotal: json['mindMapTotal'], 
      mindMapUsed: json['mindMapUsed'], 
      expirationAt: json['expirationAt']);
}
